package com.itfreer.power.service.role;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.form.api.BaseDao;
import com.itfreer.form.api.imp.DictionaryServiceImp;
import com.itfreer.form.dictionary.base.DictionaryItem;
import com.itfreer.power.dao.role.RoleDao;
import com.itfreer.power.entity.role.RoleEntity;
import com.itfreer.power.service.power.strategy.RoleDataDataProvide;
import com.itfreer.power.service.power.strategy.RoleMapExtentDataProvide;
import com.itfreer.power.service.power.strategy.RoleMapLayersDataProvide;
import com.itfreer.power.service.power.strategy.RoleMenusDataProvide;
import com.itfreer.power.service.power.strategy.RoleServiceDataProvide;

@Component("roleService")
public class RoleServiceBase extends DictionaryServiceImp<RoleEntity> implements RoleService {

	@Autowired
	private RoleDao roleDao;
	@Autowired(required = false)
	private RoleServiceDataProvide roleServiceDataProvide;
	@Autowired(required = false)
	private RoleMenusDataProvide roleMenusDataProvide;
	@Autowired(required = false)
	private RoleMapLayersDataProvide roleMapLayersDataProvide;
	@Autowired(required = false)
	private RoleMapExtentDataProvide roleMapExtentDataProvide;
	@Autowired(required = false)
	private RoleDataDataProvide roleDataDataProvide;

	/**
	 * 角色数据缓存
	 */
	@Autowired(required = false)
	private RoleDataCache roleDataCache;

	private void cleareCache(String roleId) {
		if (roleServiceDataProvide != null) {
			roleServiceDataProvide.clearCache(roleId);
		}
		if (roleMenusDataProvide != null) {
			roleMenusDataProvide.clearCache(roleId);
		}
		if (roleMapLayersDataProvide != null) {
			roleMapLayersDataProvide.clearCache(roleId);
		}
		if (roleMapExtentDataProvide != null) {
			roleMapExtentDataProvide.clearCache(roleId);
		}
		if (roleDataDataProvide != null) {
			roleDataDataProvide.clearCache(roleId);
		}
		initRoleCache();
	}

	/**
	 * 删除用户角色关系
	 * 
	 * @param userId
	 * @param roleId
	 */
	public void delUserRole(String userId, String roleId) {
		roleDao.delUserRole(userId, roleId);
	}

	/**
	 * 添加用户角色关系
	 * 
	 * @param userId
	 * @param roleId
	 */
	public void addUserRole(String userId, String roleId) {
		roleDao.addUserRole(userId, roleId);
	}

	@Override
	protected BaseDao<RoleEntity> getDao() {
		return roleDao;
	}

	@Override
	public String getDictionaryName() {
		return "p_role";
	}

	/**
	 * 添加实体
	 * 
	 * @param entity
	 *            添加实体
	 * @return
	 */
	@Override
	public RoleEntity add(RoleEntity entity) {
		entity = super.add(entity);
		// 处理分组为pid项
		addPItem(entity);
		cleareCache(entity.getId());
		return entity;
	}

	/**
	 * 更新实体 不支持更新
	 * 
	 * @param guid
	 *            唯一值
	 * @param entity
	 *            实体
	 * @return
	 */
	@Override
	public RoleEntity update(RoleEntity entity) {
		entity = super.update(entity);
		// 处理分组为pid项
		addPItem(entity);
		cleareCache(entity.getId());
		return entity;
	}

	/**
	 * 删除实体
	 * 
	 * @param guid
	 *            唯一值
	 */
	@Override
	public void delete(String guid) {
		super.delete(guid);
		cleareCache(guid);
	}

	/**
	 * 指量删除实体
	 * 
	 * @param guid
	 *            唯一值集
	 */
	@Override
	public void deletes(String guids) {
		super.deletes(guids);
		String[] ids = guids.split(",");
		for (String id : ids) {
			cleareCache(id);
		}
	}

	@Override
	protected DictionaryItem convert(RoleEntity t) {
		if (dictionaryCache == null) {
			return null;
		}

		DictionaryItem item = new DictionaryItem();
		item.setId(t.getId());
		item.setPid(t.getGroupName());
		item.setCode(t.getCode());
		item.setDisplay(t.getDisplay());
		item.setValid(t.getValid());
		item.setOrder(t.getOrder());
		item.setLeaf(true);
		item.setInfo(t.getInfo());
		item.getOther().put("isUserRegisterDefulatRole", t.getIsUserRegisterDefulatRole());
		item.getOther().put("isUserDefulatRole", t.getIsUserDefulatRole());
		item.getOther().put("registerService", t.getRegisterService());

		return item;
	}

	@Override
	protected String getDictionaryFields() {
		return "id,display,code,valid,info,groupName,order,isUserRegisterDefulatRole,isUserDefulatRole,registerService";
	}

	@PostConstruct
	@Override
	public void initDictionary() {
		if (dictionaryCache == null) {
			return;
		}

		List<RoleEntity> list = getDao().getEntitys(null, null, null, 500000, 1);
		if (list != null && list.size() > 0) {
			for (RoleEntity entity : list) {
				// 处理分组为pid项
				addPItem(entity);
				// 处理项
				dictionaryCache.add(this.getDictionaryName().toLowerCase(), convert(entity));
			}
		}

		initRoleCache();
	}

	private void addPItem(RoleEntity entity) {
		String pid = entity.getGroupName();
		DictionaryItem pItem = dictionaryCache.getDictionary(getDictionaryName(), pid);
		if (pItem == null) {
			pItem = new DictionaryItem();
			pItem.setId(pid);
			pItem.setPid("0");
			pItem.setCode(pid);
			pItem.setDisplay(pid);
			pItem.setValid(true);
			pItem.setOrder(entity.getOrder());
			pItem.setLeaf(false);
			dictionaryCache.add(getDictionaryName().toLowerCase(), pItem);
		}
	}

	private void initRoleCache() {
		if (roleDataCache == null) {
			return;
		}
		
		roleDataCache.clear();
		// 初始化角色数据缓存
		List<DictionaryItem> items = dictionaryCache.getChild(getDictionaryName(), "0");
		if(items==null || items.size()<=0) {
			return;
		}
		for (DictionaryItem item : items) {
			List<DictionaryItem> citems = dictionaryCache.getChild(getDictionaryName(), item.getId());
			if(citems==null || citems.size()<=0) {
				return;
			}
			for (DictionaryItem citem : citems) {
				roleDataCache.add(citem.getId(), citem.getOther().get("isUserRegisterDefulatRole"),
						citem.getOther().get("isUserDefulatRole"), citem.getOther().get("registerService"));
			}
		}
	}
}
